# -*-coding:utf-8 -*-
from lxml import etree
import requests
import re
import  os
from urllib.request import urlretrieve
head={"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"}
str1=['https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=59&start={}'.format(i) for i in range(0,100) if i%12==0]
str='https://www.pearvideo.com/'
for i in str1:
    print("当前网页的url为:")
    print(i)
    response=requests.get(i,headers=head)
    src=r'<a href="(.*?)" class="vervideo-lilink actplay">'
    voide_id=re.findall(src,response.text)
    print(voide_id)
    list=[]
    for i in voide_id:
        list.append(str+i)
    print("该网页的所有视频地址;")
    print(list)
    for i in list:
        response1=requests.get(i,headers=head)
        url='srcUrl="(.*?)",vdoUrl='
        all=re.findall(url,response1.text)
        print("视频的mp4地址:")
        print(all)
        urll='<h1 class="video-tt">(.*?)</h1>'
        al=re.findall(urll,response1.text)
        print("正在下载:  %s"%(al[0]))
        #在当前目录下创建一个文件夹
        path="video"
        #判断video是否在当前路径下
        if path not in os.listdir():
            os.mkdir(path)
        #path为文件路径，后面为名字和格式
        try:
            filepath=path+'/%s.mp4'%(al[0])
            urlretrieve(all[0],filepath)
        except OSError:
            print("文件读取错误")
